/*** 
This do-file executes all the scripts necessary to produce the Small Business 
Revenue ZIP-level maps.
***/

*-------------------------------------------------------------------------------
* Set up
*-------------------------------------------------------------------------------

* Set $root 
project figstabs, root
local build_running = r(buildrunning)
if (`build_running' == 0) include "${root}/code/config_interactive.do"

* Set globals
include "${root}/code/set_globals.do"

* Create required subfolders
cap mkdir "${root}/data/derived"
cap mkdir "${root}/data/derived/R packages"
cap mkdir "${root}/data/derived/Shapefiles"
cap mkdir "${root}/results/Small Business Revenue"

*-------------------------------------------------------------------------------
* Create input data for maps 
*-------------------------------------------------------------------------------
project, do("${root}/code/figs/Small Business Revenue/Small Business Revenue - Create ZIP-level for NY, SF and Chicago maps.do")

*-------------------------------------------------------------------------------
* Unzip all R packages in DVC for maps 
*-------------------------------------------------------------------------------
cd "${root}/data/derived/R packages"

local r_packages: dir "${root}/data/dvc/R packages" files "*.zip"

foreach package of local r_packages {
	project, uses("${root}/data/dvc/R packages/`package'")
	unzipfile "${root}/data/dvc/R packages/`package'", replace
}

*-------------------------------------------------------------------------------
* Unzip shapefiles in DVC for maps
*-------------------------------------------------------------------------------
cd "${root}/data/derived/Shapefiles"

project, uses("${root}/data/dvc/Shapefiles/gz_2010_us_860_00_500k.zip")
unzipfile "${root}/data/dvc/Shapefiles/gz_2010_us_860_00_500k.zip", replace

*-------------------------------------------------------------------------------
* Execute R code to create maps
*-------------------------------------------------------------------------------
 
project, uses("${root}/code/figs/Small Business Revenue/Small Business Revenue - Change in Small Business Revenue Maps.R")
project, uses("${root}/data/derived/Small Business Revenue/Map - Change in Small Business Revenue, ZIP.csv")

* First arg: code location; second arg: whether the build is running; third arg: root directory
shell R < "${root}/code/figs/Small Business Revenue/Small Business Revenue - Change in Small Business Revenue Maps.R" "`build_running'" "${root}" --no-save

* Inform the project that the maps have been created
project, creates("${root}/results/Small Business Revenue/map_smallbiz_byZIP_Chicago.png")
project, creates("${root}/results/Small Business Revenue/map_smallbiz_byZIP_NYC.png")
project, creates("${root}/results/Small Business Revenue/map_smallbiz_byZIP_SF.png")
